package com.photoeditor.demo.ui.widget.blur;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;

import com.common.base.utils.DeviceUtils;

/**
 * Created by winn on 17/5/16.
 */

public class RadialBlurTipView extends View {
    private Paint mPaint;
    private float mFocusX;
    private float mFocusY;
    private float mRadiu;

    public RadialBlurTipView(Context context) {
        super(context);
        init();
    }

    public RadialBlurTipView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RadialBlurTipView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeWidth(DeviceUtils.dip2px(getContext(), 2));
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.WHITE);
        PathEffect effects = new DashPathEffect(new float[]{DeviceUtils.dip2px(getContext(), 8), DeviceUtils.dip2px(getContext(), 4)}, 0);
        mPaint.setPathEffect(effects);
    }

    public void onShowBlurEffect(float radiu, float focusX, float focusY) {
        mFocusX = focusX;
        mFocusY = focusY;
        mRadiu = radiu;
        invalidate();
    }

    public void onUpdateCircleRadiu(float radiu) {
        mRadiu = radiu;
        invalidate();
    }

    public void updateBlurExcludeCircleRadiu(float radiu, float focusX, float focusY) {
        mRadiu = radiu;
        mFocusX = focusX;
        mFocusY = focusY;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mFocusX == -1) {
            mFocusX = getWidth() / 2;
        }
        if (mFocusY == -1) {
            mFocusY = getHeight() / 2;
        }
        canvas.drawCircle(mFocusX, mFocusY, mRadiu * getWidth() / 2, mPaint);
    }
}
